<template>
  <div class="card border-shadow">
    <div class="card-content">
      <div id="movie" class="sort" v-if="category === '热门电影'">
        <div class="movie-list-header">
          <span class="movie-list-title">热门电影</span>
          <div class="movie-list-subject-block">
            <span
              :class="['movie-list-subject', tab === '' ? 'filmActive' : '']"
              @click="changeTab()"
              >热门</span
            ><span
              :class="['movie-list-subject', tab === 'jqp' ? 'filmActive' : '']"
              @click="changeTab('jqp')"
              >剧情</span
            ><span
              :class="['movie-list-subject', tab === 'dzp' ? 'filmActive' : '']"
              @click="changeTab('dzp')"
              >动作</span
            ><span
              :class="['movie-list-subject', tab === 'khp' ? 'filmActive' : '']"
              @click="changeTab('khp')"
              >科幻</span
            ><span
              :class="['movie-list-subject', tab === 'aqp' ? 'filmActive' : '']"
              @click="changeTab('aqp')"
              >爱情</span
            ><span
              :class="['movie-list-subject', tab === 'xjp' ? 'filmActive' : '']"
              @click="changeTab('xjp')"
              >喜剧</span
            ><span
              :class="['movie-list-subject', tab === 'kbp' ? 'filmActive' : '']"
              @click="changeTab('kbp')"
              >恐怖</span
            ><span
              :class="['movie-list-subject', tab === 'zzp' ? 'filmActive' : '']"
              @click="changeTab('zzp')"
              >战争</span
            ><span
              :class="['movie-list-subject', tab === 'fzp' ? 'filmActive' : '']"
              @click="changeTab('fzp')"
              >犯罪</span
            ><span
              :class="['movie-list-subject', tab === 'jlp' ? 'filmActive' : '']"
              @click="changeTab('jlp')"
              >记录</span
            >
          </div>
        </div>
        <div
          class="movie-list-body infinite-list-wrapper"
          v-infinite-scroll="loadMore"
          infinite-scroll-disabled="disabled"
          v-loading.fullscreen.lock="loading"
        >
          <div
            class="movie-list-item"
            v-for="(item, index) in filmDatas"
            :key="index"
          >
            <a
              :href="'/search?key=' + item.name"
              target="_blank"
              rel="noreferrer"
            >
              <div class="movie-post-wrapper">
                <div class="movie-post-lazyload"></div>
                <img :src="item.poster" class="movie-post" />
              </div>
              <div class="movie-info">
                <div class="movie-title">{{ item.name }}</div>
              </div>
            </a>
          </div>
        </div>
      </div>

      <div id="tv" class="sort" v-if="category === '热播新剧'">
        <div class="movie-list-header">
          <span class="movie-list-title">热播新剧</span>
          <div class="movie-list-subject-block">
            <span
              :class="['movie-list-subject', tab === '' ? 'filmActive' : '']"
              @click="changeTab"
              >热门</span
            ><span
              :class="['movie-list-subject', tab === 'dlj' ? 'filmActive' : '']"
              @click="changeTab('dlj')"
              >国产剧</span
            ><span
              :class="['movie-list-subject', tab === 'mj' ? 'filmActive' : '']"
              @click="changeTab('mj')"
              >美剧</span
            ><span
              :class="['movie-list-subject', tab === 'hj' ? 'filmActive' : '']"
              @click="changeTab('hj')"
              >韩剧</span
            ><span
              :class="['movie-list-subject', tab === 'rj' ? 'filmActive' : '']"
              @click="changeTab('rj')"
              >日剧</span
            ><span
              :class="['movie-list-subject', tab === 'gj' ? 'filmActive' : '']"
              @click="changeTab('gj')"
              >港剧</span
            >
            <span
              :class="['movie-list-subject', tab === 'tj' ? 'filmActive' : '']"
              @click="changeTab('tj')"
              >台剧</span
            >
            <span
              :class="['movie-list-subject', tab === 'tj2' ? 'filmActive' : '']"
              @click="changeTab('tj2')"
              >泰剧</span
            >
          </div>
        </div>
        <div
          class="movie-list-body infinite-list-wrapper"
          v-infinite-scroll="loadMore"
          infinite-scroll-disabled="disabled"
          v-loading.fullscreen.lock="loading"
        >
          <div
            class="movie-list-item"
            v-for="item in varietyDatas"
            :key="item.mid"
          >
            <a
              :href="'/search?key=' + item.name"
              target="_blank"
              rel="noreferrer"
            >
              <div class="movie-post-wrapper">
                <div class="movie-post-lazyload"></div>
                <img :src="item.poster" class="movie-post" />
              </div>
              <div class="movie-info">
                <div class="movie-title">{{ item.name }}</div>
              </div>
            </a>
          </div>
        </div>
      </div>

      <div id="anime" class="sort" v-if="category === '热播动漫'">
        <div class="movie-list-header">
          <span class="movie-list-title">热播动漫</span>
        </div>
        <div
          class="movie-list-body infinite-list-wrapper"
          v-infinite-scroll="loadMore"
          infinite-scroll-disabled="disabled"
          v-loading.fullscreen.lock="loading"
        >
          <div
            class="movie-list-item"
            v-for="item in comicDatas"
            :key="item.mid"
          >
            <a
              :href="'/search?key=' + item.name"
              target="_blank"
              rel="noreferrer"
            >
              <div class="movie-post-wrapper">
                <div class="movie-post-lazyload"></div>
                <img :src="item.poster" class="movie-post" />
              </div>
              <div class="movie-info">
                <div class="movie-title">{{ item.name }}</div>
              </div>
            </a>
          </div>
        </div>
      </div>

      <div id="show" class="sort" v-if="category === '热播综艺'">
        <div class="movie-list-header">
          <span class="movie-list-title">热播综艺</span>
        </div>
        <div
          class="movie-list-body infinite-list-wrapper"
          v-infinite-scroll="loadMore"
          infinite-scroll-disabled="disabled"
          v-loading.fullscreen.lock="loading"
        >
          <div
            class="movie-list-item"
            v-for="item in dramaDatas"
            :key="item.mid"
          >
            <a
              :href="'/search?key=' + item.name"
              target="_blank"
              rel="noreferrer"
            >
              <div class="movie-post-wrapper">
                <div class="movie-post-lazyload"></div>
                <img :src="item.poster" class="movie-post" />
              </div>
              <div class="movie-info">
                <div class="movie-title">{{ item.name }}</div>
              </div>
            </a>
          </div>
        </div>
      </div>

      <div id="documentry" class="sort" v-if="category === '热播纪录片'">
        <div class="movie-list-header">
          <span class="movie-list-title">热播纪录片</span>
        </div>
        <div
          class="movie-list-body infinite-list-wrapper"
          v-infinite-scroll="loadMore"
          infinite-scroll-disabled="disabled"
          v-loading.fullscreen.lock="loading"
        >
          <div
            class="movie-list-item"
            v-for="item in documentaryDatas"
            :key="item.mid"
          >
            <a
              :href="'/search?key=' + item.name"
              target="_blank"
              rel="noreferrer"
            >
              <div class="movie-post-wrapper">
                <div class="movie-post-lazyload"></div>
                <img :src="item.poster" class="movie-post" />
              </div>
              <div class="movie-info">
                <div class="movie-title">{{ item.name }}</div>
              </div>
            </a>
          </div>
        </div>
      </div>

      <div id="top250" class="sort" v-if="category === '豆瓣Top250'">
        <div class="movie-list-header">
          <span class="movie-list-title">豆瓣Top250</span>
        </div>
        <div
          class="movie-list-body infinite-list-wrapper"
          v-infinite-scroll="loadMore"
          infinite-scroll-disabled="disabled"
          v-loading.fullscreen.lock="loading"
        >
          <div class="movie-list-item" v-for="item in topDatas" :key="item.mid">
            <a
              :href="'/search?key=' + item.name"
              target="_blank"
              rel="noreferrer"
            >
              <div class="movie-post-wrapper">
                <div class="movie-post-lazyload"></div>
                <img :src="item.poster" class="movie-post" />
              </div>
              <div class="movie-info">
                <div class="movie-title">{{ item.name }}</div>
              </div>
            </a>
          </div>
        </div>
      </div>

      <p class="tc" v-if="loading">加载中...</p>
      <p class="tc" v-if="noMore">没有更多了</p>
    </div>
  </div>
</template>

<script>
export default {
  name: "list",
  layout: "default",
  data() {
    return {
      category: "",

      filmDatas: [],
      varietyDatas: [],
      comicDatas: [],
      dramaDatas: [],
      documentaryDatas: [],
      topDatas: [],

      tab: "",
      pn: 1,
      pages: 20,
      loading: false,
    };
  },
  computed: {
    noMore() {
      return this.pn >= this.pages;
    },
    disabled() {
      return this.loading || this.noMore;
    },
  },
  mounted() {
    this.category = this.$route.query.category;
    this.loadData();
  },
  methods: {
    loadData() {
      this.loading = true;
      switch (this.category) {
        case "热门电影":
          this.getFilmDatas();
          break;
        case "热播新剧":
          this.getVarietyDatas();
          break;
        case "热播动漫":
          this.getComicDatas();
          break;
        case "热播综艺":
          this.getDramaDatas();
          break;
        case "热播纪录片":
          this.getDocumentaryDatas();
          break;
        case "豆瓣Top250":
          this.getTopDatas();
          break;
      }
    },
    loadMore() {
      if (!this.noMore) {
        this.loadData();
      }
    },
    changeTab(tab = "") {
      this.tab = tab;
      this.pn = 1;
      if (this.category === "热门电影") {
        this.filmDatas = [];
      } else {
        this.varietyDatas = [];
      }
      this.loadData();
    },
    getFilmDatas() {
      this.$axios
        .$get("/main/api/film/", {
          params: { pn: this.pn, lm: 96, tab: this.tab },
        })
        .then((data) => {
          this.filmDatas = [...this.filmDatas, ...data];
          this.loading = false;
          this.pn = this.pn + 1;
        });
    },
    getVarietyDatas() {
      this.$axios
        .$get("/main/api/variety/", {
          params: { pn: this.pn, lm: 96, tab: this.tab },
        })
        .then((data) => {
          this.varietyDatas = [...this.varietyDatas, ...data];
          this.loading = false;
          this.pn = this.pn + 1;
        });
    },
    getComicDatas() {
      this.$axios
        .$get("/main/api/comic/", { params: { pn: this.pn, lm: 96 } })
        .then((data) => {
          this.comicDatas = [...this.comicDatas, ...data];
          this.loading = false;
          this.pn = this.pn + 1;
        });
    },
    getDramaDatas() {
      this.$axios
        .$get("/main/api/drama/", { params: { pn: this.pn, lm: 96 } })
        .then((data) => {
          this.dramaDatas = [...this.dramaDatas, ...data];
          this.loading = false;
          this.pn = this.pn + 1;
        });
    },
    getDocumentaryDatas() {
      this.$axios
        .$get("/main/api/documentary/", { params: { pn: this.pn, lm: 96 } })
        .then((data) => {
          this.documentaryDatas = [...this.documentaryDatas, ...data];
          this.loading = false;
          this.pn = this.pn + 1;
        });
    },
    getTopDatas() {
      this.$axios
        .$get("/main/api/top/", { params: { pn: this.pn, lm: 96 } })
        .then((data) => {
          this.topDatas = [...this.topDatas, ...data];
          this.loading = false;
          this.pn = this.pn + 1;
        });
    },
  },
};
</script>
<style>
html,
body {
  overflow: hidden;
}
.el-carousel__container {
  position: relative;
  height: 300px;
}

.el-carousel__container img {
  width: 100%;
}

.card {
  margin-top: 15px;
  width: 100%;
  min-height: 0;
  padding: 5px 10px 10px;
  box-sizing: border-box;
  overflow: hidden;
}
.border-shadow {
  box-shadow: 0 8px 33px 0 rgb(30 35 42 / 5%);
  border-radius: 8px;
  background: #fff;
  box-sizing: border-box;
}
.card-content {
  box-sizing: border-box;
}
.home-nav,
.nav {
  display: flex;
  align-items: center;
}
.home-nav {
  height: 55px;
  width: 100%;
  font-size: 15px;
}
.nav {
  width: 50%;
  justify-content: center;
  height: 100%;
  cursor: pointer;
}
a {
  color: #175199;
  text-decoration: none;
  outline-style: none;
  cursor: pointer;
}

.search-result-container,
.sort {
  margin-top: 20px;
}

.movie-list-header {
  width: 100%;
  margin: 0 -10px;
  padding: 0 10px;
  background: hsla(0, 0%, 100%, 0.9);
  -webkit-backdrop-filter: saturate(180%) blur(30px);
  backdrop-filter: saturate(180%) blur(30px);
}

.movie-list-more {
  float: right;
  padding: 2px 6px 2px 10px;
  margin-top: 10px;
  align-items: center;
  cursor: pointer;
  text-align: center;
  border: 1px solid silver;
  border-radius: 2px;
  font-size: 14px;
  color: #505050;
  transition: all 0.2s;
  line-height: 14px;
}

.movie-list-more .iconfont {
  font-size: 15px;
}

.movie-list-title {
  font-size: 24px;
  font-weight: 700;
  line-height: 2;
}

.movie-list-subject-block {
  white-space: nowrap;
  box-sizing: border-box;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  padding-bottom: 3px;
}

.movie-list-subject {
  font-size: 17px;
  padding: 4px 8px;
  margin-right: 10px;
  background: #f3f3f4;
  border-radius: 15px;
  white-space: nowrap;
  line-height: 2.2;
  box-sizing: border-box;
  cursor: pointer;
}
.movie-list-subject {
  font-size: 16px;
  padding: 4px 8px;
  margin-right: 10px;
  background: #f3f3f4;
  border-radius: 15px;
  white-space: nowrap;
  line-height: 2.2;
  box-sizing: border-box;
  cursor: pointer;
}

.search-result-container,
.sort {
  margin-top: 20px;
}

.movie-list-item {
  width: 14.6667%;
  padding: 10px 1% 3px;
  font-size: 15px;
  cursor: pointer;
  border-radius: 8px;
}

.movie-list-body {
  display: flex;
  flex-wrap: wrap;
  background: none;
  line-height: 100%;
  margin: 0 -1% -8px;
  box-sizing: border-box;
  height: 68vh;
  overflow-y: auto;
  overflow-x: hidden;
}

.movie-post-wrapper {
  position: relative;
  overflow: hidden;
  color: transparent;
}

.movie-post-lazyload {
  padding-top: 140%;
  background: url()
    50% / cover no-repeat;
  border-radius: 8px;
}

.filmActive,
.filmActive:hover {
  color: #fff;
  background: #2fb3db;
}

.movie-title {
  font-size: 16px;
  font-weight: 700;
  line-height: 1.5;
  color: rgba(0, 0, 0, 0.9);
  margin-top: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.movie-post {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: 8px;
  -o-object-fit: cover;
  object-fit: cover;
}
.el-rate__item {
  font-size: 0;
  vertical-align: middle;
}
.el-rate__icon,
.el-rate__item {
  position: relative;
  display: inline-block;
}
.el-rate {
  height: 32px;
  line-height: 1;
}
.el-rate__icon {
  font-size: 24px;
  margin-right: 6px;
  color: #c0c4cc;
}
.tc {
  text-align: center;
  font-size: 14px;
  color: #666;
  margin-top: 20px;
}
@media screen and (max-width: 800px) {
  .el-carousel__container {
    position: relative;
    height: 180px;
  }
  .movie-list-item {
    width: 31.3333%;
    padding: 10px 1% 10px;
    font-size: 15px;
    cursor: pointer;
    border-radius: 8px;
  }
  .el-rate {
    height: 30px;
  }
  .el-rate__icon {
    font-size: 8px;
    margin-right: 0;
  }
}
</style>
